'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskAddjoinpeopleCtrl
 * @description
 * # TaskAddjoinpeopleCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskAddjoinpeopleCtrl', function(openApi, DataService, $location, $routeParams, Util) {
        this.peopleIdObj = {};
        // 单选或者多选
        var vm = this;
        this.type = $routeParams.type
        this.title = (this.type == "participator" || this.type=="participator1") ? "选择参与人" : "选择负责人";
        this.peopleIdObjfromArr = function() {
            var param = {};
            param = Util.getParamForReview("TaskNewtaskCtrl");
            var peopleObj = {};
            if (param.joinPersons && param.joinPersons.length > 0) {
                param.joinPersons.forEach(function(item, index) {
                    var userId = item.userId;
                    vm.peoplelist.forEach(function(peritem, perindex) {
                        if (peritem.userId == userId) {
                            peopleObj[perindex] = true;
                            return
                        }
                    })
                });
            }
            return peopleObj
        };
        this.handleUser = function(item, index) {
            var paramData = Util.getParamForReview("TaskNewtaskCtrl");
            if (this.type == "participator1") {
                var param = {},
                    url;
                var userid = item.userId
                param.refId = paramData.pkId
                param.userId = userid;
                if(this.peopleIdObj[index]){
                    url = openApi.addJoinPerson
                }else{
                    url = openApi.delJoinPerson
                }
                DataService.getData(url, param).then(function(data) {}.bind(this), function() {
                    this.peopleIdObj[index] = !this.peopleIdObj[index]
                }.bind(this));
            }
        };
        this.getcheckedPoople = function() {
            var param = {};
            param = Util.getParamForReview("TaskNewtaskCtrl");
            // 过滤负责人
            if (this.type == "incharge") {
                this.peopleIdObj.userId = param.userId;
                this.peopleIdObj.userName = param.userName;
                this.filterinchargeList(param.joinPersons);
                // 过滤参与人
            } else if (this.type == "participator" || this.type == "participator1") {
                // 过滤子任务负责人（需要从负责人以及参与人中选择）
                this.filterparticipatorList(param.userId);
                this.peopleIdObj = this.peopleIdObjfromArr();
            } else if (this.type == "subtask") {
                this.filterSubPeopleList();
            }
        };
        this.confirm = function() {
            var param = {},
                data = {},
                daraArr = [];
            param = Util.getParamForReview("TaskNewtaskCtrl")
                // data.type = this.type
            if (this.type == "incharge") {
                // 负责人
                param.userId = JSON.parse(this.peopleIdObj).userId;
                param.userName = JSON.parse(this.peopleIdObj).userName;
                if (!param.userId) {
                    Util.Toast("请选择负责人");
                    return
                }
            } else if (this.type == "participator" || this.type == "participator1") {
                // 如果新建任务参与人或者任务详情参与人（任务详情参与人需要实时发请求到后台）
                for (var i in this.peopleIdObj) {
                    if (this.peopleIdObj[i]) {
                        daraArr.push(this.peoplelist[i]);
                    }
                }
                if (daraArr.length < 1) {
                    Util.Toast("请选择参与人成员");
                    return
                }
                param.joinPersons = daraArr;
            } else if (this.type == "subtask") {
                // 子任务负责人
                var subtaskObj = {};
                subtaskObj = Util.getLocalInfo("subtaskObj")
                subtaskObj.userId = JSON.parse(this.peopleIdObj).userId;
                subtaskObj.userName = JSON.parse(this.peopleIdObj).userName;
                if (!subtaskObj.userId) {
                    Util.Toast("请选择负责人");
                    return
                }
                Util.saveLocalInfo("subtaskObj", subtaskObj)
            }
            Util.savaParamForReview(param, "TaskNewtaskCtrl")
            Util.goBack();
        }
        this.filterinchargeList = function(ids) {
            if (ids) {
                ids.forEach(function(item, index) {
                    var userId = item.userId;
                    vm.filterparticipatorList(userId);
                })
            }
        };
        this.filterparticipatorList = function(id) {
            vm.peoplelist.forEach(function(peoitem, peoIndex) {
                if (id == peoitem.userId) {
                    vm.peoplelist.splice(peoIndex, 1);
                    return
                }
            })
        };
        this.filterSubPeopleList = function() {
            var param = {};
            var userIdArr = [];
            param = Util.getParamForReview("TaskNewtaskCtrl");
            if (param.joinPersons) {
                userIdArr = param.joinPersons.map(function(item, index) {
                    return item.userId;
                });
            }
            var userId = param.userId;
            userIdArr.push(userId);
            this.peoplelist = this.peoplelist.filter(function(item, index) {
                return userIdArr.indexOf(item.userId) > -1
            });
        };
        this.initData = function() {
            this.getUsers();
        }
        this.getUsers = function() {
            var param = {};
            param.cache = true;
            DataService.getData(openApi.getUsers, param).then(function(data) {
                // 获取数据列表
                this.peoplelist = data;
                // 获取已经选择的列表
                this.getcheckedPoople();
            }.bind(this))
        }
        this.initData();
    });
